<?php

namespace App\Http\Controllers\Api;

use App\Commission;
use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CommissionController extends Controller
{
    use ApiResponse;
    public $uid;
    public function __construct()
    {
        $this->uid=auth('api')->id();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
       $list=Commission::with(['order','user'])->where('uid',$this->uid)->where('type',0)->paginate($request->get('size',10));
       $commission_list=Commission::where('uid',$this->uid)->get();
       $can_withdraw=$commission_list->where('status',1)->sum("commission");
       $cannot_withdraw=$commission_list->where('status',0)->sum("commission");
//       $withdraw_amount=$commission_list->where('type',1)->sum("commission");
//       $user_number=$commission_list->where('type',0)->groupBy('order_uid')->count();
//       $order_number=$commission_list->where('type',0)->count();
       $result=[
           'can_withdraw'=>round($can_withdraw,2),
           'cannot_withdraw'=>round($cannot_withdraw,2),
           'list'=>$list
       ];
       return $this->data($result);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function withdraw(Request $request)
    {
//        $money=$request->get('money');
//        if ($money<100){
//            return $this->message_error("提现金额需多于100元");
//        }
        $total_amount=Commission::where('uid',$this->uid)->where('status',1)->sum("commission");
        $money=$total_amount;
//        if ($total_amount<$money){
//            return $this->message_error("金额不足，请重新输入");
//        }
        if ($money<100){
            return $this->message_error("可提现金额需满足100元");
        }
        $app=app('wechat.payment');
        $withDrawNumber="T".Carbon::now()->format('YmdHis') . random_int(1000, 9999) . $this->uid;
        $result=$app->transfer->toBalance([
            'partner_trade_no' => $withDrawNumber, // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
            'openid' => auth('api')->user()['openid'],
            'check_name' => 'NO_CHECK', // NO_CHECK：不校验真实姓名, FORCE_CHECK：强校验真实姓名
            're_user_name' => '提现', // 如果 check_name 设置为FORCE_CHECK，则必填用户真实姓名
            'amount' => floatval($money)*100, // 企业付款金额，单位为分
            'desc' => '提现', // 企业付款操作说明信息。必填
        ]);
        if ($result['return_code']==="SUCCESS"){
            if ($result['result_code']=="SUCCESS"){
                $data=[
                    'uid'=>$this->uid,
                    'order_number'=>$withDrawNumber,
                    'order_uid'=>$this->uid,
                    'commission'=>-$money,
                    'type'=>1
                ];
                Commission::create($data);
                return $this->message_success("提现申请成功，金额稍后到账");
            }else{
                return $this->message_error("金额错误，无法提现");
            }

        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function withdraw_list(Request $request)
    {
        $list=Commission::where('uid',$this->uid)->where('type',1)->paginate($request->get('size',10));
        $total_amount=Commission::where('uid',$this->uid)->where('type',1)->sum("commission");
        $result=[
            'total_amount'=>round($total_amount,2),
            'list'=>$list
        ];
        return $this->data($result);
    }
    public function query_withdraw()
    {
        $app=app('wechat.payment');
        $partnerTradeNo = "T2020090312093415371";
        $result=$app->transfer->queryBalanceOrder($partnerTradeNo);
        dd($result);
    }
    public function commission_cms()
    {
        $commission_list=Commission::where('uid',$this->uid)->get();
        $can_withdraw=$commission_list->where('status',1)->sum("commission");
        $cannot_withdraw=$commission_list->where('status',0)->sum("commission");
        $withdraw_amount=$commission_list->where('type',1)->sum("commission");
        $user_number=$commission_list->where('type',0)->groupBy('order_uid')->count();
        $order_number=$commission_list->where('type',0)->count();
        $result=[
            'can_withdraw'=>round($can_withdraw,2),
            'cannot_withdraw'=>round($cannot_withdraw,2),
            "withdraw_amount"=>round($withdraw_amount,2),
            "user_number"=>$user_number,
            'order_number'=>$order_number,
        ];
        return $this->data($result);
    }
    public function commission_user(Request $request)
    {
        $commission_list=Commission::with('user')->where('uid',$this->uid)->groupBy('order_uid')->paginate($request->get('size',10));
        return $this->data($commission_list);
    }
}
